package com.oa.cms.service.impl;

import com.oa.cms.converter.CmsVersionConverter;
import com.oa.cms.dao.CmsVersionRepository;
import com.oa.cms.domain.dto.CmsVersionDto;
import com.oa.cms.domain.entity.CmsVersion;
import com.oa.cms.domain.enums.Status;
import com.oa.cms.query.CmsVersionQuery;
import com.oa.cms.query.base.Page;
import com.oa.cms.service.CmsVersionService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * Created by gold-jx on 2017/12/1.
 */
@Service
public class CmsVersionServiceImpl implements CmsVersionService {

    @Autowired
    private CmsVersionRepository cmsVersionRepository;

    @Autowired
    private CmsVersionConverter cmsVersionConverter;


    @Override
    public int save(CmsVersion cmsVersion) {
        if (StringUtils.isEmpty(cmsVersion.getStatus())) {
            cmsVersion.setStatus(Status.NORMAL);
        }
        cmsVersion.setCreateTime(new Date());
        return StringUtils.isEmpty(cmsVersionRepository.save(cmsVersion)) ? 0 : 1;
    }

    @Override
    public int delete(Long id) {
        cmsVersionRepository.delete(id);
        return 1;
    }

    @Override
    public int update(CmsVersion cmsVersion) {

        return StringUtils.isEmpty(cmsVersionRepository.save(cmsVersion)) ? 0 : 1;
    }

    @Override
    public Page<CmsVersionDto> list(CmsVersionQuery cmsVersionQuery) {

        PageRequest pageable = new PageRequest(StringUtils.isEmpty(cmsVersionQuery.getStart()) ? 0 : cmsVersionQuery.getStart(),
                StringUtils.isEmpty(cmsVersionQuery.getPageSize()) ? 10 : cmsVersionQuery.getPageSize());

        org.springframework.data.domain.Page<CmsVersion> page = cmsVersionRepository.findAll(new Specification<CmsVersion>() {
            @Override
            public Predicate toPredicate(Root<CmsVersion> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                //模糊查询
                /*if (StringUtils.isNoneBlank(model.getName())) {
                    list.add(cb.like(root.get("name").as(String.class), "%" + model.getName() + "%"));
                }*/
                if (!StringUtils.isEmpty(cmsVersionQuery.getVersion())) {
                    list.add(criteriaBuilder.equal(root.get("version").as(String.class), cmsVersionQuery.getVersion()));
                }
                if (!StringUtils.isEmpty(cmsVersionQuery.getVersionType())) {
                    list.add(criteriaBuilder.equal(root.get("versionType").as(String.class), cmsVersionQuery.getVersionType()));
                }
                if (!StringUtils.isEmpty(cmsVersionQuery.getStatus())) {
                    list.add(criteriaBuilder.equal(root.get("status").as(Status.class), cmsVersionQuery.getStatus()));
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        }, pageable);

        int count = Integer.parseInt(page.getTotalElements() + "");

        List<CmsVersionDto> cmsVersionDtos = null;
        if (count > 0) {
            cmsVersionDtos = cmsVersionConverter.convertToDto(page.getContent());
        }

        if (CollectionUtils.isEmpty(cmsVersionDtos)) {
            return new Page(count, cmsVersionDtos);
        }
        return new Page<>(count, cmsVersionDtos);
    }
}
